#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QtGui/QMouseEvent>
#include <QtGui/QApplication>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    InitData();
    InitUI();
    InitSignals();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::CreatItem(){



}

void MainWindow::InitData(){

    m_Moveing = false;

}

void MainWindow::InitUI(){

    //设置无边框
    setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint |Qt::WindowStaysOnTopHint);
    this->setStyleSheet("QDialog{border:2px solid green;}");

}

void MainWindow::InitSignals(){


}

//重写鼠标按下事件
void MainWindow::mousePressEvent(QMouseEvent *event)
{
    QPoint clickPotion = event->globalPos() - pos();

    if(clickPotion.y() > 30 || event->button() != Qt::LeftButton){
        //限制拖动区域 左键
        return;
    }

    m_Moveing = true;
    //记录下鼠标相对于窗口的位置
    m_MovePosition = clickPotion;
    return QMainWindow::mousePressEvent(event);
}

//重写鼠标移动事件
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if (m_Moveing &&
        (event->globalPos()-m_MovePosition).manhattanLength() > QApplication::startDragDistance())
    {
        move(event->globalPos()-m_MovePosition);
        m_MovePosition = event->globalPos() - pos();
    }
    return QMainWindow::mouseMoveEvent(event);
}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    m_Moveing = false;
}

